package cn.csc.module.shoppingCard.dal.mysql.transaction;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.shoppingCard.controller.admin.transaction.vo.CardTransactionExportReqVO;
import cn.csc.module.shoppingCard.controller.admin.transaction.vo.CardTransactionGroupByCardIdsRespVO;
import cn.csc.module.shoppingCard.controller.admin.transaction.vo.CardTransactionPageReqVO;
import cn.csc.module.shoppingCard.dal.dataobject.transaction.CardTransactionDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * 交易记录 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface CardTransactionMapper extends BaseMapperX<CardTransactionDO> {

    /**
     * 交易记录分页查询（过滤掉status=0）
     * @param reqVO
     * @return
     */
    default PageResult<CardTransactionDO> selectPage(CardTransactionPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CardTransactionDO>()
                .eqIfPresent(CardTransactionDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(CardTransactionDO::getCardId, reqVO.getCardId())
                .eqIfPresent(CardTransactionDO::getTxnType, reqVO.getTxnType())
                .eqIfPresent(CardTransactionDO::getRelatedOrder, reqVO.getRelatedOrder())
                .eq(CardTransactionDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(CardTransactionDO::getTxnTime, reqVO.getTxnTime())
                .eqIfPresent(CardTransactionDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(CardTransactionDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CardTransactionDO::getCreateTime));
    }

    default List<CardTransactionDO> selectList(CardTransactionExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CardTransactionDO>()
                .eqIfPresent(CardTransactionDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(CardTransactionDO::getCardId, reqVO.getCardId())
                .eqIfPresent(CardTransactionDO::getTxnType, reqVO.getTxnType())
                .eqIfPresent(CardTransactionDO::getAmount, reqVO.getAmount())
                .eqIfPresent(CardTransactionDO::getRelatedOrder, reqVO.getRelatedOrder())
                .eq(CardTransactionDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(CardTransactionDO::getTxnTime, reqVO.getTxnTime())
                .eqIfPresent(CardTransactionDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(CardTransactionDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CardTransactionDO::getCreateTime));
    }

    default List<CardTransactionDO> selectByCardId(BigInteger cardId) {
        return selectList(new LambdaQueryWrapperX<CardTransactionDO>()
                .eq(CardTransactionDO::getCardId, cardId)
                .eq(CardTransactionDO::getStatus, (byte) 1)
                .orderByDesc(CardTransactionDO::getCreateTime)
        );
    }

    default List<CardTransactionDO> selectByOrderItemIds(List<BigInteger> orderItemIds) {
        return selectList(new LambdaQueryWrapperX<CardTransactionDO>()
                .in(CardTransactionDO::getRelatedOrderItem, orderItemIds)
        );
    }

    default List<CardTransactionDO> selectByOrderIds(List<BigInteger> orderIds) {
        return selectList(new LambdaQueryWrapperX<CardTransactionDO>()
                .in(CardTransactionDO::getRelatedOrder, orderIds)
        );
    }

    /**
     * 分组查询
     * @param cardIds 购物卡主键
     * @param txnType 交易类型（1-充值，2-消费，3-退款，4-冻结，5-解冻）
     * @param status 状态(0-创建，1-完成)
     * @return
     */
    @MapKey("cardId")
    List<CardTransactionGroupByCardIdsRespVO> groupByCardIds(@Param("cardIds") List<BigInteger> cardIds, @Param("txnType") Integer txnType, @Param("status") Integer status);

    /**
     * 更新交易记录状态和交易时间
     */
    default int updateStatus(Collection<BigInteger> cardIds, List<BigInteger> orderIds, Byte status) {
        LambdaUpdateWrapper<CardTransactionDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CardTransactionDO::getTxnTime, LocalDateTime.now());
        updateWrapper.set(CardTransactionDO::getStatus, status);
        updateWrapper.in(CardTransactionDO::getCardId, cardIds);
        updateWrapper.in(CardTransactionDO::getRelatedOrder, orderIds);
        return update(null, updateWrapper);
    }

}
